/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Users;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Sony
 */
public class UserHelper {
    Session session;
    public UserHelper(){
        session = HibernateUtil.getSessionFactory().getCurrentSession();
    }
    
    public List<Users> getAllUser(){
        List<Users> lstUser = null;
        try {
            org.hibernate.Transaction tx = this.session.beginTransaction();
            Query q = this.session.createQuery("from Users");
            lstUser = (List<Users>)q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lstUser;
    }
    
    public List<Users> getUserBy_UserName_Password(String UserName, String Password){
        List<Users> lstUser = null;
        try {
            org.hibernate.Transaction tx = this.session.beginTransaction();
            Query q = this.session.createQuery("from Users l where l.username ='" +UserName+"' and "+
                    "l.password = '" + Password+"'");
            lstUser = (List<Users>)q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lstUser;
    }
    
    public void updateUser(Users u){
        try {
            org.hibernate.Transaction tx = this.session.beginTransaction();
            this.session.save(u);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
      public void deleteUser(Users u){
        try {
            org.hibernate.Transaction tx = this.session.beginTransaction();
            this.session.delete(u);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
